<template>
  <el-row type="flex" justify="center" align="middle" class="wrapper">
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="手机号:">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码:">
        <el-input v-model="form.password" type="password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="login">登录</el-button>
      </el-form-item>
    </el-form>
  </el-row>
</template>

<script>
export default {
  data() {
    return {
      form: {
        username: "",
        password: "",
      },
    };
  },
  methods: {
    // 登录事件
    login() {
      // 判断用户名和密码是否为空
      if (!this.form.username || !this.form.password) {
        this.$message.warning("请输入完整的信息");
        return;
      }
      // 发起请求
      this.$axios({
        method: "post",
        url: "/login",
        data: this.form,
      }).then((res) => {
        // console.log(res.data);
        // 登录成功
        if (res.data.message == "登录成功") {
          // 保存本地数据
          localStorage.setItem("token", res.data.data.token);
          localStorage.setItem("userId", JSON.stringify(res.data.data.user));
          // 提示登录成功
          this.$message.success(res.data.message);
          // 跳转页面
          this.$router.push("/");
        } else {
          // 登录失败
          this.$message.error(res.data.message);
        }
      });
    },
    // 取消事件,清空用户名和密码
    cancel() {
      this.form = {};
    },
  },
};
</script>

<style lang="less" scoped>
.wrapper {
  height: 100vh;
  background: url("../assets/timg (1).jpg") center;
}
</style>
